package logic

import (
	"context"
	"errors"
	"product/model/mysql"

	"product/productrpc/internal/svc"
	"product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *productrpc.ShopAddRequest) (*productrpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line

	var Shop mysql.Shop
	mysql.DB.Where("shop_name = ?", in.ShopName).Find(&Shop)
	if Shop.ID != 0 {
		return nil, errors.New("当前商品已存在，添加失败")
	}

	shop := mysql.Shop{
		ShopName:  in.ShopName,
		ShopPrice: float64(in.ShopPrice),
	}

	err := mysql.DB.Debug().Create(&shop).Error
	if err != nil {
		return nil, err
	}

	return &productrpc.ShopAddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
